<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class Client extends Frontend
{

    protected $layout = 'default';
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 问题反馈
    **/
    public function feedback(){
        $this->assign('feedlist',build_select('row[sel]',['电话拒接','电话空号或停机','客户不需要','客户有意向'],0,['class' => 'form-control selectpicker']));
        return $this->view->fetch();
    }

    /**
     * 客户管理
    **/
    public function index(){
        $page=input('get.page/d');
        $data=model('apply')->where([])->order('id desc')->paginate(10,'',['page'=>$page]);
        echo db()->getLastSql();
//        return $this->view->fetch('',['page'=>$data]);
    }

    /**
     * 详情
    **/
    public function detail(){
        $id = input('id/d',0);

        if ( !$id ){
            $this->error('参数错误');
        }

        // 获取积分
        // 判断是不是父级还是子级
        $user = model('user');
        if ($this->auth->parent_id != 0){
            // 要扣除的id
            $uid = $this->auth->parent_id;
            $score = $user->getscore($this->auth->parent_id);
        }else{
            $uid = $this->auth->id;
            $score = $user->getscore($this->auth->id);
        }

        // 获取详情
        $apply = model('apply')->get($id);

        // 如果已经被购买过了
        if($apply->status == 1){
            $this->error('已经被认领过了');
        }

        // 大于580 的扣除4分
        if ($apply->zhimafen >= 580){
            $deduct = 4;
        }else{
            //小于于580 的扣除3分
            $deduct = 3;
        }

        // 如果积分不够
        if ( !($score >= $deduct) ){
            $this->error('积分不足哦');
        }

        // 扣除积分
        $user->where('id',$uid)->setDec('score',$deduct);

        // 添加订单
//        $addData = [
//            'apply_id'=>$id,
//            'user_id'=>$uid,
//            'score'=>$deduct,
//            'apply_info'=>serialize($apply->toArray())
//        ];
//        model('applyBet')->save($addData);

        // 完成订单
//        model('apply')->where('id',$id)->update(['status'=>1]);

        // 记录积分
        dump(model('ScoreDetail')->select());
        return $this->view->fetch('',['row'=>$apply]);

    }
}
